package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.Project;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class ProjectDao {
	private DBHelp<Project> db = new DBHelp<Project>();
	
	private class ProjectMap implements RowMapper<Project>{

		@Override
		public Project mapperRow(ResultSet rs) throws SQLException {
			Project project = new Project();
			
			project.setId(rs.getInt("id"));
			project.setName(rs.getString("name"));
			project.setDesc(rs.getString("desc"));
			project.setCreateTime(rs.getString("createtime"));
			
			return project;
		}
		
	}


	public List<Project> findRelProjectByEmployeeId(int id) {
		String sql = "SELECT t_project.id AS id, `name`, `desc`, createtime FROM t_project, t_project_employee WHERE t_project.id = t_project_id AND t_employee_id = ? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new ProjectMap(), id);
	}


	public int insertProject(String proName, String desc, String createTime) {
		String sql = "INSERT INTO t_project(name, `desc`, createtime) VALUES(?, ?, ?)";
		return db.getAutoKey(sql, proName, desc, createTime);
	}


	public Project findProById(String curpid) {
		String sql = "SELECT id ,name,`desc`,createtime from t_project where id = ?";
		return db.executeQueryForObject(sql, new ProjectMap(), curpid);
	}


	public Project getProByEmpIdAndProId(int nameId, int proId) {
		String sql = "SELECT id, `name`, `desc`, createtime FROM t_project, t_project_employee WHERE t_project.id = t_project_id AND (t_project_id = ? AND t_employee_id = ?)";
		return db.executeQueryForObject(sql, new ProjectMap(), proId, nameId);
	}
}
